package com.test.daily.leetcode.y2021.m12.day1210;

import java.util.Arrays;

/**
 * @descriptions: shortestCompletingWord
 * @author: Tom
 * @date: 2021/12/10 下午 01:10
 * @version: 1.0
 */
public class Code01_ShortestCompletingWord {
    public static void main(String[] args) {
        Code01_ShortestCompletingWord cs = new Code01_ShortestCompletingWord();
        String licensePlate = "TE73696";
        String[] words = new String[]{"ten","two","better","talk","suddenly","stand","protect","collection","about","southern"};
        String ans = cs.shortestCompletingWord(licensePlate, words);
        System.out.println(ans.toString());
    }
    public String shortestCompletingWord(String licensePlate, String[] words) {
        String licensePlateCopy = licensePlate.toLowerCase();
        int[] licensePlateCount = new int[26];
        for(int i = 0; i < licensePlateCopy.length(); i++){
            if(licensePlateCopy.charAt(i)>= 'a' && licensePlateCopy.charAt(i)<= 'z'){
                licensePlateCount[licensePlateCopy.charAt(i) - 'a']++;
            }

        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < words.length; i++){
            int[] licensePlateCountCopy = Arrays.copyOf(licensePlateCount, licensePlateCount.length);
            for(int j = 0; j < words[i].length(); j++){
                licensePlateCountCopy[words[i].charAt(j) - 'a'] --;
            }
            boolean flag = true;
            for(int j = 0; j < 26; j++){
                if(licensePlateCountCopy[j] > 0) {
                    flag = false;
                    break;
                }
            }
            if(flag){
                if(min == Integer.MAX_VALUE){
                    min = i;
                } else {
                    if(words[min].length() > words[i].length()){
                        min = i;
                    } else if(words[min].length() == words[i].length() && (min - i > 0)){
                        min = i;
                    }
                }
            }
        }
        return words[min];
    }
}
